home *** CD-ROM | disk | FTP | other *** search
/ Graphics & Sound Program…ng Techniques for the Mac / Graphics and Sound Programming Techniques for the Mac.iso / M&T Graphics & Sound Examples / Symantec Versions / C02 Sound Playing / P03 Sound Commands / SoundCommands.c next >
Encoding:
C/C++ Source or Header  |  1995-08-31  |  4.1 KB  |  169 lines  |  [TEXT/KAHL]

  1. //____________________________________________________________
  2. //    SoundCommands.c
  3. //
  4. //    Copyright © Dan Parks Sydow, 1995
  5. //    From the book:
  6. //    "Graphics and Sound Programming Techniques for the Mac",
  7. //    M&T Books, 1995
  8. //
  9. //    The "Graphics and Sound Programming for the Mac" book shows the call to SndPlay()
  10. //    with a different second parameter than the one used in this code. The book shows the
  11. //    SndPlay() call as expected by the version of the Sound.h universal header file used
  12. //    by Metrowerks at the time of this writing. Symantec uses a different version of 
  13. //    Sound.h. If Symantec updates the Apple Universal Header files, you may get a compilation 
  14. //    error. If you do, it will concern the call to SndPlay() in the PlaySoundResourceSynch() function.
  15. //    Here is the change you'll need to make. Change
  16. //    FROM:
  17. //
  18. //         theError = SndPlay( theChannel, theHandle, false );
  19. //
  20. //    TO:
  21. //
  22. //         theError = SndPlay( theChannel, (SndListHandle)theHandle, false );
  23.  
  24.  
  25.  
  26. //____________________________________________________________
  27.  
  28. #include <Sound.h>
  29.  
  30.  
  31. //____________________________________________________________
  32.  
  33. void           InitializeToolbox( void );
  34. SndChannelPtr  OpenOneSynchSoundChannel( void );
  35. OSErr          DisposeOneSoundChannel( SndChannelPtr );
  36. OSErr          PlaySoundResourceSynch( SndChannelPtr, short );
  37. OSErr          SetSoundAmplitude( SndChannelPtr, short );
  38.  
  39. //____________________________________________________________
  40.  
  41. #define    rPoliceSiren      9000
  42.  
  43.  
  44. //____________________________________________________________
  45.  
  46. void  main( void )
  47. {
  48.    NumVersion     theSndMgrVers;
  49.    short          theResID;
  50.    OSErr          theError;
  51.    SndChannelPtr  theChannel;
  52.    short          theAmplitude;
  53.       
  54.    InitializeToolbox();
  55.  
  56.    theSndMgrVers = SndSoundManagerVersion();   
  57.    if ( theSndMgrVers.majorRev < 3 )
  58.       ExitToShell();
  59.    
  60.    theChannel = OpenOneSynchSoundChannel();
  61.    if ( theChannel == nil )
  62.       ExitToShell();
  63.  
  64.    theAmplitude = 50;
  65.    theError = SetSoundAmplitude( theChannel, theAmplitude );
  66.    if ( theError != noErr )
  67.       ExitToShell();
  68.  
  69.    theResID = rPoliceSiren;
  70.    theError = PlaySoundResourceSynch( theChannel, theResID );
  71.    if ( theError != noErr )
  72.       ExitToShell();
  73.    
  74.    theError = DisposeOneSoundChannel( theChannel );
  75.    if ( theError != noErr )
  76.       ExitToShell();
  77. }
  78.  
  79.  
  80. //____________________________________________________________
  81.  
  82. OSErr  SetSoundAmplitude( SndChannelPtr theChannel, short theAmp )
  83. {
  84.    SndCommand  theCommand;
  85.    OSErr       theError;
  86.    
  87.    theCommand.cmd = ampCmd;
  88.    theCommand.param1 = theAmp;
  89.    theCommand.param2 = 0;
  90.    
  91.    theError = SndDoCommand( theChannel, &theCommand, false );
  92.    
  93.    return ( theError );
  94. }
  95.  
  96.  
  97. //____________________________________________________________
  98.  
  99. SndChannelPtr  OpenOneSynchSoundChannel( void )
  100. {
  101.    SndChannelPtr  theChannel;   
  102.    OSErr          theError;
  103.    
  104.    theChannel = nil;
  105.    theError = SndNewChannel( &theChannel, 0, 0, nil );
  106.    
  107.    if ( theError != noErr )
  108.    {
  109.       DisposePtr( (Ptr)theChannel );
  110.       theChannel = nil;
  111.    }
  112.       
  113.    return ( theChannel );
  114. }
  115.  
  116.  
  117. //____________________________________________________________
  118.  
  119. OSErr  DisposeOneSoundChannel( SndChannelPtr theChannel )
  120. {
  121.    OSErr  theError;
  122.    
  123.    theError = SndDisposeChannel( theChannel, true );
  124.    DisposePtr( (Ptr)theChannel );
  125.    
  126.    return ( theError );
  127. }
  128.  
  129.  
  130. //____________________________________________________________
  131.  
  132. OSErr  PlaySoundResourceSynch( SndChannelPtr theChannel, short theResID )
  133. {
  134.    Handle  theHandle;
  135.    OSErr   theError;
  136.    
  137.    theHandle = GetResource( 'snd ', theResID );
  138.    
  139.    if ( theHandle == nil )
  140.    {   
  141.       return ( resProblem );
  142.    }
  143.    else
  144.    {
  145.       HLock( theHandle );
  146.          theError = SndPlay( theChannel, theHandle, false );
  147.       HUnlock( theHandle );
  148.    
  149.       ReleaseResource( theHandle );
  150.  
  151.       return ( theError );
  152.    }
  153. }
  154.  
  155.  
  156. //____________________________________________________________
  157.  
  158. void  InitializeToolbox( void )
  159. {
  160.    InitGraf( &qd.thePort );
  161.    InitFonts();
  162.    InitWindows();
  163.    InitMenus();
  164.    TEInit();
  165.    InitDialogs( 0L );
  166.    FlushEvents( everyEvent, 0 );
  167.    InitCursor();
  168. }
  169.